package com.kmxd.ams.app.archive;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmxd.ams.client.archive.IArchiveRuleAttachmentSvc;
import com.kmxd.ams.client.archive.dto.ArchiveRuleAttachmentAddDTO;
import com.kmxd.ams.client.archive.dto.ArchiveRuleAttachmentEditDTO;
import com.kmxd.ams.client.archive.dto.ArchiveRuleAttachmentQueryDTO;
import com.kmxd.ams.client.archive.vo.ArchiveRuleAttachmentListVO;
import com.kmxd.ams.client.archive.vo.ArchiveRuleAttachmentVO;
import com.kmxd.ams.core.common.query.QueryGen;
import com.kmxd.ams.core.util.IdUtil;
import com.kmxd.ams.infra.archive.entity.ArchiveRuleAttachment;
import com.kmxd.ams.infra.archive.mapper.ArchiveRuleAttachmentMapper;
import java.util.List;
import java.util.Set;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 附件匹配管理 服务实现类
 *
 * @author nickbi
 * @since 2022-07-30
 */
@Service
@AllArgsConstructor
public class ArchiveRuleAttachmentSvcImpl
    extends ServiceImpl<ArchiveRuleAttachmentMapper, ArchiveRuleAttachment>
    implements IArchiveRuleAttachmentSvc {

  private ArchiveRuleAttachmentMapper archiveRuleAttachmentMapper;

  @Override
  public Page<ArchiveRuleAttachmentListVO> selectPage(
      Page reqPage, ArchiveRuleAttachmentQueryDTO req) {

    LambdaQueryWrapper<ArchiveRuleAttachment> queryWrapper =
        QueryGen.init(new QueryWrapper<ArchiveRuleAttachment>(), req).lambda();
    Page<ArchiveRuleAttachment> page =
        archiveRuleAttachmentMapper.selectPage(reqPage, queryWrapper);
    Page<ArchiveRuleAttachmentListVO> voPage = BeanUtil.copyProperties(page, Page.class);
    voPage.setRecords(BeanUtil.copyToList(page.getRecords(), ArchiveRuleAttachmentListVO.class));
    return voPage;
  }

  @Override
  public List<ArchiveRuleAttachmentListVO> selectList(ArchiveRuleAttachmentQueryDTO req) {

    LambdaQueryWrapper<ArchiveRuleAttachment> queryWrapper =
        QueryGen.init(new QueryWrapper<ArchiveRuleAttachment>(), req).lambda();
    List<ArchiveRuleAttachment> list = archiveRuleAttachmentMapper.selectList(queryWrapper);
    return BeanUtil.copyToList(list, ArchiveRuleAttachmentListVO.class);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void add(ArchiveRuleAttachmentAddDTO req) {
    ArchiveRuleAttachment entity = BeanUtil.copyProperties(req, ArchiveRuleAttachment.class);
    archiveRuleAttachmentMapper.insert(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void edit(ArchiveRuleAttachmentEditDTO req) {
    ArchiveRuleAttachment entity = BeanUtil.copyProperties(req, ArchiveRuleAttachment.class);
    archiveRuleAttachmentMapper.updateById(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void remove(String ids) {
    Set<Long> idList = IdUtil.split(ids);
    this.baseMapper.deleteBatchIds(idList);
  }

  @Override
  public ArchiveRuleAttachmentVO view(Long id) {
    ArchiveRuleAttachment entity = archiveRuleAttachmentMapper.selectById(id);
    return BeanUtil.copyProperties(entity, ArchiveRuleAttachmentVO.class);
  }
}
